package com.olda.play.auth.security.userdetails.wechat;

import com.olda.play.enums.ResponseCode;
import com.olda.play.model.UserDetailInfo;
import com.olda.play.rpc.CommonResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * UserDetailsService只负责从特定的地方加载用户信息，可以是数据库、redis缓存、接口等
 */
@Service("wechatUserDetailsService")
@RequiredArgsConstructor
public class WechatUserDetailsServiceImpl implements UserDetailsService {

   /* @Reference
    private LoginService loginService;*/


    /**
     * 根据用户名获取用户信息
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        CommonResponseDto<UserDetailInfo> commonResponseVO=loginService.getUserDetailInfoByUserName(username);
        CommonResponseDto<UserDetailInfo> commonResponseVO=null;
        return commonResponseVO.getData();
    }


    /**
     * 手机号码认证方式
     *
     * @param mobile
     * @return
     */
    public UserDetails loadUserByMobile(String mobile) {
        WechatUserDetails userDetails = null;
//        CommonResponseDto<UserDetailInfo> commonResponseVO=loginService.getUserDetailInfoByUserName(mobile);
        CommonResponseDto<UserDetailInfo> commonResponseVO=null;
        if (commonResponseVO.getCode().equals(ResponseCode.SUCCESS.getCode())) {
            UserDetailInfo member = commonResponseVO.getData();
            if (null != member) {
//                userDetails = new WechatUserDetails(member);
//                userDetails.setAuthenticationIdentity(AuthenticationIdentityEnum.MOBILE.getValue());   // 认证身份标识:mobile
            }
        }
        if (userDetails == null) {
            throw new UsernameNotFoundException(ResponseCode.USER_NOT_EXIST.getMessage());
        } else if (!userDetails.isEnabled()) {
            throw new DisabledException("该账户已被禁用!");
        } else if (!userDetails.isAccountNonLocked()) {
            throw new LockedException("该账号已被锁定!");
        } else if (!userDetails.isAccountNonExpired()) {
            throw new AccountExpiredException("该账号已过期!");
        }
        return userDetails;
    }

    /**
     * openid 认证方式
     *
     * @param openId
     * @return
     */
    public UserDetails loadUserByOpenId(String openId) {
        WechatUserDetails userDetails = null;
//        CommonResponseDto<UserDetailInfo> commonResponseVO=loginService.getUserDetailInfoByUserName(openId);
        CommonResponseDto<UserDetailInfo> commonResponseVO=null;

        if (commonResponseVO.getCode().equals(ResponseCode.SUCCESS.getCode())) {
            UserDetailInfo member = commonResponseVO.getData();
            if (null != member) {
//                userDetails = new WechatUserDetails(member);
//                userDetails.setAuthenticationIdentity(AuthenticationIdentityEnum.OPENID.getValue());   // 认证身份标识:openId
            }
        }
        if (userDetails == null) {
            throw new UsernameNotFoundException(ResponseCode.USER_NOT_EXIST.getMessage());
        } else if (!userDetails.isEnabled()) {
            throw new DisabledException("该账户已被禁用!");
        } else if (!userDetails.isAccountNonLocked()) {
            throw new LockedException("该账号已被锁定!");
        } else if (!userDetails.isAccountNonExpired()) {
            throw new AccountExpiredException("该账号已过期!");
        }
        return userDetails;
    }
}
